package com.goods.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.ISupplierMapper;
import com.goods.business.service.ISupplierService;
import com.goods.common.error.SystemCodeEnum;
import com.goods.common.error.SystemException;
import com.goods.common.model.business.Supplier;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA
 *
 * @Date: 2023/3/6 - 14:34
 * @Author: Hyoung
 * @Description:
 */
@Service
public class SupplierServiceImpl implements ISupplierService {

    @Autowired
    private ISupplierMapper iSupplierMapper;

    @Override
    public List<SupplierVO> findAll() {
        List<Supplier> suppliers = iSupplierMapper.selectAll();
        List<SupplierVO> collect = suppliers.stream().map(supplier -> {
            SupplierVO supplierVO = new SupplierVO();
            BeanUtils.copyProperties(supplier, supplierVO);
            return supplierVO;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public void updateSupplier(Integer id, SupplierVO supplierVO) {
        supplierVO.setId(Long.valueOf(id));
        Supplier supplier = new Supplier();
        supplierVO.setModifiedTime(new Date());
        BeanUtils.copyProperties(supplierVO, supplier);

        iSupplierMapper.updateByPrimaryKey(supplier);
    }

    @Override
    public SupplierVO findSupplierById(Integer id) {
        Supplier supplier = iSupplierMapper.selectByPrimaryKey(id);
        SupplierVO supplierVO = new SupplierVO();
        BeanUtils.copyProperties(supplier, supplierVO);
        return supplierVO;
    }

    @Override
    public void addSupplier(SupplierVO supplierVO) {
        Supplier supplier = new Supplier();
        supplierVO.setCreateTime(new Date());
        supplierVO.setModifiedTime(new Date());
        BeanUtils.copyProperties(supplierVO, supplier);
        iSupplierMapper.insert(supplier);
    }

    @Override
    @SneakyThrows
    public void deleteSupplierById(Integer id) {
        Supplier supplier = iSupplierMapper.selectByPrimaryKey(id);
        if (Objects.isNull(supplier)) {
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR, "要删除的物资来源不存在");
        }
        iSupplierMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageVO<SupplierVO> findSupplierList(Integer pageNum, Integer pageSize, SupplierVO supplierVO) {
        PageHelper.startPage(pageNum, pageSize);
        Example example = new Example(Supplier.class);
        // TODO 前端没有字段
        String address = supplierVO.getAddress(); // 省市区
        String contact = supplierVO.getContact(); // 联系人姓名
        String name = supplierVO.getName(); // 具体地址
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(contact)) {
            criteria.andLike("contact", "%" + contact + "%");
        }
        if (!StringUtils.isEmpty(address)) {
            criteria.andLike("address", "%" + address + "%");

        }
        if (!StringUtils.isEmpty(name)) {
            criteria.andLike("name", "%" + name + "%");
        }

        List<Supplier> suppliers = iSupplierMapper.selectByExample(example);
        List<SupplierVO> collect = suppliers.stream().map(supplier -> {
            SupplierVO supplierVOTemp = new SupplierVO();
            BeanUtils.copyProperties(supplier, supplierVOTemp);
            return supplierVOTemp;
        }).collect(Collectors.toList());
        PageInfo<Supplier> supplierPageInfo = new PageInfo<>(suppliers);
        return new PageVO<>(supplierPageInfo.getTotal(), collect);
    }
}
